package com.jt.Controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JedisCluster jedisCluster;

    @RequestMapping("/getMsg")
    public String getMsg(){
        return "京淘注册登录模块正常";
    }

    /**
     * 1.url请求地址: http://sso.jt.com/user/check/{param}/{type}
     * 2.请求参数:    {需要校验的数据}/{校验的类型是谁}
     * 3.返回值结果:  SysResult返回  需要包含true/false
     * 4.JSONP请求方式:  返回值必须经过特殊的格式封装 callback(json)
     */
    @RequestMapping("/check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param, @PathVariable Integer type, String callback){
        //1.校验数据库中是否存在该数据
        Boolean flag = userService.checkUser(param,type);
        //int a = 1/0;
        return new JSONPObject(callback,SysResult.success(flag));
    }


    //测试HtpClient
    @RequestMapping("/httpClient/saveUser")
    public SysResult saveUser(User user){
        userService.saveHtpClient(user);
        return SysResult.success();
    }

    /**
     * 登录后用户名回显
     * url地址：http://sso.jt.com/user/query/e745a732b2b2412f991aad461910e236?callback=jsonp1598001534676&_=1598001534805
     * @return
     */
    @RequestMapping("/query/{ticket}")
    public JSONPObject findUserByTicket(@PathVariable String ticket,String callback){
        String userJSON = jedisCluster.get(ticket);
        if (StringUtils.isEmpty(userJSON)){
            return new JSONPObject(callback,SysResult.fail());
        }else {
            return new JSONPObject(callback,SysResult.success(userJSON));
        }
    }
}
